package cn.com.fssg.platform.base.handler.impl.gsl.jc;

import cn.com.fssg.platform.base.handler.InterfaceHandler;
import cn.com.fssg.platform.biz.BizUtil;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * @Description: 工商联届次修改执委在职离职状态数据接口
 */
@Component
public class GslJcZwJobCtl implements InterfaceHandler {

    private NamedParameterJdbcTemplate jdbcTemplate = BizUtil.cur().tenantMainJdbcTemplate();
//    @Autowired
//    private NamedParameterJdbcTemplate jdbcTemplate;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Object run(HttpServletRequest request, JSONObject jsonObject) throws Exception {
        String zwId = jsonObject.getString("zw_id");
        String queryZwSql = "select * from wx_user where id = :id";
        Map<String, Object> zwUserParam = new HashMap<>();
        zwUserParam.put("id", zwId);
        List<Map<String, Object>> zwUsers = jdbcTemplate.queryForList(queryZwSql, zwUserParam);

        String queryZwByParentIdSql = "select * from wx_user where parent_id = :id";
        Map<String, Object> parentParam = new HashMap<>();
        parentParam.put("id", zwId);
        List<Map<String, Object>> parentUsers = jdbcTemplate.queryForList(queryZwByParentIdSql, parentParam);
        if(!zwUsers.isEmpty()){
            Map<String, Object> wxUser = zwUsers.get(0);
            Map<String, Object> dlUser = !parentUsers.isEmpty() ? parentUsers.get(0) : new HashMap<>();
            String jobState = wxUser.get("job_state").toString();
            if (StringUtils.equals(jobState, "0")) {
                if(!parentUsers.isEmpty()){
                    dlUser.put("job_state", "1");
                    dlUser.put("update_date", new Date());
                    dlUser.put("quit_date", null);
                    dlUser.put("update_by", BizUtil.cur().curUserId());
                }
                wxUser.put("job_state", "1");
                wxUser.put("update_date", new Date());
                wxUser.put("quit_date", null);
                wxUser.put("update_by", BizUtil.cur().curUserId());
            } else {
                if(!parentUsers.isEmpty()){
                    dlUser.put("job_state", "0");
                    dlUser.put("update_date", new Date());
                    dlUser.put("quit_date", new Date());
                    dlUser.put("update_by", BizUtil.cur().curUserId());
                }
                wxUser.put("job_state", "0");
                wxUser.put("update_date", new Date());
                wxUser.put("quit_date", new Date());
                wxUser.put("update_by", BizUtil.cur().curUserId());
            }
            String wxUpdateUserSql = "update wx_user set " +
                    "job_state = :job_state, update_date = :update_date, quit_date = :quit_date, update_by = :update_by " +
                    "where id = :id";
            jdbcTemplate.update(wxUpdateUserSql, wxUser);
            if(!parentUsers.isEmpty()) {
                jdbcTemplate.update(wxUpdateUserSql, dlUser);
            }
        }
        return null;
    }

}
